package com.hiof.location;

import com.hiof.overlays.MapPointsManager;
import com.hiof.servercommunication.receive.beans.GeoPoint;
import com.hiof.settings.SettingManager;
import com.hiof.utility.Utility;

/**
 * Created with IntelliJ IDEA.
 * User: manish
 * Date: 4/17/12
 * Time: 6:50 PM
 * To change this template use File | Settings | File Templates.
 */
public class SimulatorGpsManager extends GPSManager {
    int counter = 0;
    private static SimulatorGpsManager instance;
    private int velocity = 0;

    public static SimulatorGpsManager getInstance() {
        if (null == instance) {
            instance = new SimulatorGpsManager();
        }
        return instance;
    }

    private SimulatorGpsManager() {
    }

    @Override
    public GeoPoint getLatestLocation() {
        GeoPoint geoPoint = MapPointsManager.getInstane().getPoint(counter);
        increaseCounter();
        fireEvent(geoPoint);
        return geoPoint;
    }

    private void increaseCounter() {
        try{
            velocity = SettingManager.getInstance().getSimulationSpeed();
        }catch (Exception ex){
            Utility.error(this.getClass().toString(), "Error when setting velocity of simulator.");
        }
        counter += velocity;
        if (counter >= MapPointsManager.getInstane().getRouteMapSize()) {
            counter = 0;
        }
    }

    @Override
    public double getLatestSpeed() {
        return SettingManager.getInstance().getSimulationSpeed();
    }

    @Override
    public void destroy(){
        counter = 0;
        if (instance != null)
            instance = null;
    }
}
